package com.victor.controller.web;

import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.victor.entity.YhUsers;
import com.victor.service.UserService;
import com.victor.utils.Msg;

@RestController
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;

	// 搜索user信息
	@RequestMapping("/search")
	public Msg getYhuserByPhone(String phone) {
		YhUsers yu = userService.selYhuserByPhone(phone);
		return Msg.success().add("user", yu);

	}

	// 获取user信息
	@RequestMapping("getAllUser")
	public Msg getAllUser() {
		List<YhUsers> userlist = userService.getAllUser();
		return Msg.success().add("userlist", userlist);
	}

	// 批量导出excel
	@RequestMapping("/yhusersExcel")
	public void batchExportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<YhUsers> yhUsersList = userService.getAllUser();

		// 创建poi导出数据对象
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		// 创建sheet页
		Sheet sheet = sxssfWorkbook.createSheet("用户列表");

		CellRangeAddress region1 = new CellRangeAddress(0, 1, (short) 0, (short) 19);
		// 参数1：起始行 参数2：终止行 参数3：起始列 参数4：终止列
		sheet.addMergedRegion(region1);
		Row headTitle = sheet.createRow(0);
		headTitle.createCell(0).setCellValue("用户列表");

		// 创建表头
		Row headRow = sheet.createRow(4);
		// 设置表头信息
		headRow.createCell(0).setCellValue("手机");
		headRow.createCell(1).setCellValue("第三方openid");
		headRow.createCell(2).setCellValue("昵称");
		headRow.createCell(3).setCellValue("邮箱");
		headRow.createCell(4).setCellValue("头像");
		headRow.createCell(5).setCellValue("性别");
		headRow.createCell(6).setCellValue("创建时间");
		headRow.createCell(7).setCellValue("登陆时间");
		headRow.createCell(8).setCellValue("登陆token");
		headRow.createCell(9).setCellValue("用户状态");
		headRow.createCell(10).setCellValue("im_token");
		headRow.createCell(11).setCellValue("等级");
		headRow.createCell(12).setCellValue("余额");
		headRow.createCell(13).setCellValue("优惠券数量");
		headRow.createCell(14).setCellValue("红包余额");
		headRow.createCell(15).setCellValue("v卡余额");
		headRow.createCell(16).setCellValue("经验值");
		headRow.createCell(17).setCellValue("学生生日");
		headRow.createCell(18).setCellValue("学生姓名");
		headRow.createCell(19).setCellValue("邀请码");

		// 遍历上面数据库查到的数据
		for (YhUsers yu : yhUsersList) {
			Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			if (yu.getPhone() == null) {
				dataRow.createCell(0).setCellValue("");
			} else {
				dataRow.createCell(0).setCellValue(yu.getPhone());
			}
			if (yu.getOpen_id() == null) {
				dataRow.createCell(1).setCellValue("");
			} else {
				dataRow.createCell(1).setCellValue(yu.getOpen_id());
			}
			if (yu.getNickname() == null) {
				dataRow.createCell(2).setCellValue("");
			} else {
				dataRow.createCell(2).setCellValue(yu.getNickname());
			}
			if (yu.getEmail() == null) {
				dataRow.createCell(3).setCellValue("");
			} else {
				dataRow.createCell(3).setCellValue(yu.getEmail());
			}
			if (yu.getAvatar() == null) {
				dataRow.createCell(4).setCellValue("");
			} else {
				dataRow.createCell(4).setCellValue(yu.getAvatar());
			}
			if (yu.getSex() == null) {
				dataRow.createCell(5).setCellValue("");
			} else {
				dataRow.createCell(5).setCellValue(yu.getSex());
			}
			if (yu.getCreate_time() == null) {
				dataRow.createCell(6).setCellValue("");
			} else {
				dataRow.createCell(6).setCellValue(yu.getCreate_time());
			}
			if (yu.getLogin_time() == null) {
				dataRow.createCell(7).setCellValue("");
			} else {
				dataRow.createCell(7).setCellValue(yu.getLogin_time());
			}
			if (yu.getAccess_token() == null) {
				dataRow.createCell(8).setCellValue("");
			} else {
				dataRow.createCell(8).setCellValue(yu.getAccess_token());
			}
			if (yu.getUser_status() == null) {
				dataRow.createCell(9).setCellValue("");
			} else {
				dataRow.createCell(9).setCellValue(yu.getUser_status());
			}
			if (yu.getIm_token() == null) {
				dataRow.createCell(10).setCellValue("");
			} else {
				dataRow.createCell(10).setCellValue(yu.getIm_token());
			}
			if (yu.getLevel_name() == null) {
				dataRow.createCell(11).setCellValue("");
			} else {
				dataRow.createCell(11).setCellValue(yu.getLevel_name());
			}
			if (yu.getBalance() == null) {
				dataRow.createCell(12).setCellValue("");
			} else {
				dataRow.createCell(12).setCellValue(yu.getBalance());
			}
			if (yu.getCoupons() == null) {
				dataRow.createCell(13).setCellValue("");
			} else {
				dataRow.createCell(13).setCellValue(yu.getCoupons());
			}
			if (yu.getBalance_red() == null) {
				dataRow.createCell(14).setCellValue("");
			} else {
				dataRow.createCell(14).setCellValue(yu.getBalance_red());
			}
			if (yu.getBalance_vcard() == null) {
				dataRow.createCell(15).setCellValue("");
			} else {
				dataRow.createCell(15).setCellValue(yu.getBalance_vcard());
			}
			if (yu.getEmpiric_value() == null) {
				dataRow.createCell(16).setCellValue("");
			} else {
				dataRow.createCell(16).setCellValue(yu.getEmpiric_value());
			}
			if (yu.getStudent_birthday() == null) {
				dataRow.createCell(17).setCellValue("");
			} else {
				dataRow.createCell(17).setCellValue(yu.getStudent_birthday());
			}
			if (yu.getStudent_name() == null) {
				dataRow.createCell(18).setCellValue("");
			} else {
				dataRow.createCell(18).setCellValue(yu.getStudent_name());
			}
			if (yu.getInvite_code() == null) {
				dataRow.createCell(19).setCellValue("");
			} else {
				dataRow.createCell(19).setCellValue(yu.getInvite_code());
			}
		}
		// 下载导出文件名
		String filename = "用户列表";
		// 设置头信息
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		// 一定要设置成xlsx格式
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
		// 创建一个输出流
		ServletOutputStream outputStream = response.getOutputStream();
		// 写入数据
		sxssfWorkbook.write(outputStream);
		// 关闭
		outputStream.close();
		sxssfWorkbook.close();

	}
}
